package com.tuanzhang.zhw.rpc.config.provider;


import com.tuanzhang.zhw.rpc.register.zookeeper.RpcRegistryProviderService;
import com.tuanzhang.zhw.rpc.server.RpcServer;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 服务启动 配置类
 */
@Slf4j
public class RpcServerConfig {

    private final String host = "127.0.0.1";
    private RpcServer rpcServer = null;
    protected int port;
    private List<ProviderConfig> providerConfigs;// 元数据信息列表
    private RpcRegistryProviderService rpcRegistryProviderService;// 注册元数据信息服务

    public RpcServerConfig(List<ProviderConfig> providerConfigs) {
        this.providerConfigs = providerConfigs;
    }

    public RpcServerConfig(List<ProviderConfig> providerConfigs, RpcRegistryProviderService rpcRegistryProviderService) {
        this.providerConfigs = providerConfigs;
        this.rpcRegistryProviderService = rpcRegistryProviderService;
    }

    public RpcServerConfig(RpcRegistryProviderService rpcRegistryProviderService) {
        this.providerConfigs = rpcRegistryProviderService.getProviderConfigList();
        this.rpcRegistryProviderService = rpcRegistryProviderService;
    }


    /**
     * rpc 服务导出
     */
    public void exporter() {
        String serverAddress = host + ":" + port;
        if (rpcServer == null) {
            try {
                this.rpcServer = new RpcServer(serverAddress);
            } catch (InterruptedException e) {
                log.error("rpc 服务导出失败", e);
            }

            // 注册服务提供者实例到 server
            providerConfigs.forEach(providerConfig -> {
                        rpcServer.registerProcessor(providerConfig);
                        //注册中心
                        providerConfig.setAddress(serverAddress);

                        if (rpcRegistryProviderService != null) {
                            try {
                                rpcRegistryProviderService.registry(providerConfig);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
            );
        }
    }

    public String getHost() {
        return host;
    }

    public RpcServer getRpcServer() {
        return rpcServer;
    }

    public void setRpcServer(RpcServer rpcServer) {
        this.rpcServer = rpcServer;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public List<ProviderConfig> getProviderConfigs() {
        return providerConfigs;
    }

    public void setProviderConfigs(List<ProviderConfig> providerConfigs) {
        this.providerConfigs = providerConfigs;
    }

    public RpcRegistryProviderService getRpcRegistryProviderService() {
        return rpcRegistryProviderService;
    }

    public void setRpcRegistryProviderService(RpcRegistryProviderService rpcRegistryProviderService) {
        this.rpcRegistryProviderService = rpcRegistryProviderService;
    }
}
